package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.LawyerList;
import com.example.smart_city_0829.entity.lawyer_onsultation.LegalExpertiseList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Response;

public class LawyerListActivity extends BaseActivity {

    private LinearLayout linearLayout4;
    private Spinner spinner;
    private Button siftBt;
    private ListView lv;
    private Button loadMoreBt;


    private LegalExpertiseList legalExpertiseList;

    private List<LawyerList.RowsBean> lawyerData;
    private LvAdapter lvAdapter;

    private int pageNum = 1;
    private String sortOf = null;
    private Integer legalExpertiseId = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lawyer_list;
    }

    @Override
    protected void initView() {
        linearLayout4 = (LinearLayout) findViewById(R.id.linearLayout4);
        spinner = (Spinner) findViewById(R.id.spinner);
        siftBt = (Button) findViewById(R.id.sift_bt);
        lv = (ListView) findViewById(R.id.lv);
        loadMoreBt = (Button) findViewById(R.id.load_more_bt);

    }

    @Override
    protected void initData() {
        final int legalExpertiseId = getIntent().getIntExtra("legalExpertiseId", 0);
        if (legalExpertiseId != 0){
            this.legalExpertiseId = legalExpertiseId;
        }
        //获取法律专长
        api.getLegalExpertiseList().enqueue(new NetCallBack<LegalExpertiseList>() {
            @Override
            public void onResponse(Call<LegalExpertiseList> call, @NonNull Response<LegalExpertiseList> response) {
                legalExpertiseList = response.body();

            }
        });

        getLawyerList();
    }

    @Override
    protected void initEvent() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                pageNum = 1;
                switch (position) {
                    case 0://默认排序
                        sortOf = null;
                        getLawyerList();
                        break;
                    case 1://从业年限
                        sortOf = "workStartAt";
                        getLawyerList();
                        break;
                    case 2://服务人数
                        sortOf = "serviceTimes";
                        getLawyerList();
                        break;
                    case 3://好评率
                        sortOf = "favorableRate";
                        getLawyerList();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //ignore
            }
        });

        siftBt.setOnClickListener(v -> {
//            showLoadingDialog(null,null, ProgressDialog.STYLE_SPINNER);
            final MaxSizeGv maxSizeGv = new MaxSizeGv(context);
            maxSizeGv.setNumColumns(4);
            maxSizeGv.setAdapter(new LegalAdapter(Objects.requireNonNull(legalExpertiseList.getRows()), context));
            final AlertDialog dialog = new AlertDialog.Builder(context)
                    .setView(maxSizeGv)
                    .create();
            maxSizeGv.setOnItemClickListener((parent, view, position, id) -> {
                //TODO 休息一下
                final LegalExpertiseList.RowsBean item = (LegalExpertiseList.RowsBean) parent.getAdapter().getItem(position);
                legalExpertiseId = item.getId();
                pageNum = 1;
                getLawyerList();
                dialog.dismiss();
            });
            dialog.show();
//            progressDialog.dismiss();
        });

        loadMoreBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum++;
                getLawyerList();
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "全部律师";
    }

    private void getLawyerList() {
        //获取律师列表
        api.getLawyerList(Constant.pageSize, pageNum, legalExpertiseId, null, sortOf).enqueue(new NetCallBack<LawyerList>() {
            @Override
            public void onResponse(Call<LawyerList> call, Response<LawyerList> response) {
                if (response.body() == null) return;
                if (pageNum == 1) {
                    lawyerData = response.body().getRows();
                    lvAdapter = new LvAdapter(lawyerData, context);
                    lv.setAdapter(lvAdapter);
                } else {
                    lawyerData.addAll(response.body().getRows());
                    lvAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private class LegalAdapter extends CommonAdapter<LegalExpertiseList.RowsBean, CommonAdapter.CommGridViewHolder> {

        public LegalAdapter(List<LegalExpertiseList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LegalExpertiseList.RowsBean data, int position, CommGridViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.serverName.setText(data.getName());
        }

        @Override
        protected CommGridViewHolder getViewHolder() {
            return new CommGridViewHolder();
        }

        @Override
        protected void initView(CommGridViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    static class LvAdapter extends CommonAdapter<LawyerList.RowsBean, CommonAdapter.CommListViewHolder2> {

        public LvAdapter(List<LawyerList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LawyerList.RowsBean data, int position, CommListViewHolder2 viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getAvatarUrl())
                    .into(viewHolder.imageView);

            viewHolder.name.setText(String.valueOf(data.getName()));
            viewHolder.workYearsTv.setText("从业" + (Calendar.getInstance().get(Calendar.YEAR) - Integer.parseInt(data.getWorkStartAt().substring(0, 4))) + "年");
            viewHolder.goodAtTv.setText("法律专长：" + data.getLegalExpertiseName());
            viewHolder.serviceTimesTv.setText("咨询人数：" + data.getServiceTimes());
            viewHolder.favorableRateTv.setText("好评率：" + data.getFavorableRate());

            viewHolder.quesBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Intent intent = new Intent(context, LawyerDetailActivity.class);
                    intent.putExtra("lawyerData", gson.toJson(data));
                    context.startActivity(intent);
                }
            });
        }

        @Override
        protected CommListViewHolder2 getViewHolder() {
            return new CommListViewHolder2();
        }

        @Override
        protected void initView(CommListViewHolder2 viewHolder, View convertView) {
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.workYearsTv = (TextView) convertView.findViewById(R.id.work_years_tv);
            viewHolder.goodAtTv = (TextView) convertView.findViewById(R.id.goodAt_tv);
            viewHolder.serviceTimesTv = (TextView) convertView.findViewById(R.id.serviceTimes_tv);
            viewHolder.favorableRateTv = (TextView) convertView.findViewById(R.id.favorableRate_tv);
            viewHolder.quesBt = (Button) convertView.findViewById(R.id.ques_bt);

        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.lawyer_list_item;
        }
    }
}